<script setup>
import { ref } from 'vue'
import CesiumMap from '@/components/CesiumMap/index.vue'
import { flyTo, zoomIn, zoomOut } from '@/script/commons'

const cMapRef = ref()
let viewer
const selectedOption = ref('img')
const options = ref([
  { id: 'img', value: 'img', text: '天地图影像' },
  { id: 'vec', value: 'vec', text: '天地图矢量' }
])
const sceneModel = ref('2D')

// 创建地图
const onCreatedMap = viewerObj => {
  viewer = viewerObj
  console.log('viewer', viewerObj)
  flyToChina()
}

// 将三维球定位到中国
const flyToChina = () => {
  const position = [103.84, 31.15, 17850000]
  flyTo(viewer, position)
}

// 地图场景切换
const changeSceneMode = () => {
  if (sceneModel.value === '2D') {
    sceneModel.value = '3D'
    viewer.scene.morphTo2D(0)
  } else if (sceneModel.value === '3D') {
    sceneModel.value = '2D'
    viewer.scene.morphTo3D(0)
  }
}

// 放大缩小
const zoomInOut = value => {
  if (value === 1) {
    // 放大
    zoomIn(viewer)
  } else {
    // 缩小
    zoomOut(viewer)
  }
}

// 切换cesium底图
const toggleMap = value => {
  cMapRef.value.toggleBottomMap(value)
}

// 使用高德poi搜索定位
const center = ref()
const searchPlace = (e, callback) => {
  AMap.plugin(['AMap.PlaceSearch'], () => {
    const placeSearch = new AMap.PlaceSearch()
    placeSearch.search(e, (status, result) => {
      if (status === 'complete') {
        callback(
          result.poiList.pois.map(i => {
            return {
              ...i,
              value: i.name
            }
          })
        )
      }
    })
  })
}
const setMapCenter = e => {
  if (e.location.lat && e.location.lng) {
    console.log([e.location.lng, e.location.lat])
    const position = [e.location.lng, e.location.lat, 10000]
    flyTo(viewer, position)
  } else {
    console.error('未找到该地址经纬度')
  }
}
</script>
<template>
  <div class="three-map">
    <CesiumMap @created="onCreatedMap" ref="cMapRef"></CesiumMap>
    <div class="btn">
      <div class="bottom-layer">
        <div v-for="option in options" :key="option.id">
          <input
            type="radio"
            :id="option.id"
            :value="option.value"
            v-model="selectedOption"
            @click="toggleMap(option.id)"
          />
          <label :for="option.id">{{ option.text }}</label>
        </div>
      </div>
      <button @click="changeSceneMode">{{ sceneModel }}</button>
      <button @click="zoomInOut(1)">放大</button>
      <button @click="zoomInOut(0)">缩小</button>
      <button @click="flyToChina">复位</button>
    </div>
    <div class="search">
      <el-autocomplete
        size="small"
        v-model="center"
        :fetch-suggestions="searchPlace"
        placeholder="输入地址快速定位"
        @select="setMapCenter"
        clearable
      ></el-autocomplete>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.three-map {
  width: 100%;
  height: 100%;
  .btn {
    position: absolute;
    top: 10px;
    right: 20px;
    .bottom-layer {
      background-color: white;
      border: 1px solid #ccc;
    }
  }
  .search {
    position: absolute;
    top: 10px;
    left: 20px;
  }
}
</style>
